﻿package igz.fleaxy.ui 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import igz.fleaxy.Fleaxy;
	import igz.fleaxy.ui.text.LabelUi;
	import igz.fleaxy.util.ObjectUtil;
	
	/**
	 * @author Intelygenz | www.intelygenz.com
	 */
	public class TooltipUi 
	{
		protected var _Target : DisplayObject;
		
		protected var _LabelUi : LabelUi;
		
		protected var _HtmlText : String;
		
		protected var _Style : String;
		
		protected var _StageWidth : uint;
		
		/* 
		 * Muestra un Tooltip básico (texto, alpha, fondo, borde y filtros) personalizable con la hoja de estilo
		 * 
		 * @param p_target		: DisplayObject sobre el que se aplicará el tooltip
		 * @param p_htmlText	: Texto que contendrá el tooltip
		 * @param p_style		: Estilo del tooltip
		 * 
		 * Estilo.tooltip {
		 *		font-family: Trebuchet;
		 *		font-size: 12;
		 *		color: #000000;
		 *		igz-antialias: advanced;
		 *		igz-background: #DEE7EA;
		 *		igz-border: #555555;
		 * 		igz-alpha: 0.9;
		 *		igz-filter-dropshadow: "angle":"45","alpha":"0.3","blurY":"2","color":"0x000000","strength":"1";
		 *	}
		 * 
		 * @example
		 * 
		 * var tooltipUi : TooltipUi = new TooltipUi( target, "Tooltip text!", "tooltip" );
		 * (no es necesario añadirlo)
		 * 
		 */		
		public function TooltipUi(p_target:DisplayObject, p_htmlText:String, p_style:String = null) {
			_Target = p_target;
			_HtmlText = p_htmlText;
			_Style = p_style;
			
			$Enable();
			p_target.addEventListener(Event.REMOVED_FROM_STAGE, _Target_OnRemovedFromStage);
		}
		
		private function _Target_OnMouseOver( p_mouseEvent:MouseEvent ):void {
			_Show();
		}
		
		private function _Show () : void {
			_LabelUi = new LabelUi( _HtmlText, _Style );
			_SetStageWidth();
			
			_Repos();
			
			Fleaxy.$Current.$Stage.addChild( _LabelUi );
			
			_Target.addEventListener(MouseEvent.MOUSE_OUT, _Target_OnMouseOut);
			_Target.addEventListener(MouseEvent.MOUSE_MOVE, _Target_OnMouseMove);
			Fleaxy.$Current.$Stage.addEventListener( Event.RESIZE, _Stage_OnResize );
			
			Fleaxy.$Current.$Stage.setChildIndex( _LabelUi, _LabelUi.parent.numChildren - 1 );
		}

		private function _Target_OnMouseMove ( p_mouseEvent:MouseEvent ) : void {
			Fleaxy.$Current.$Stage.setChildIndex( _LabelUi, _LabelUi.parent.numChildren - 1 );
			_Repos();
		}
		
		private function _Target_OnMouseOut ( p_mouseEvent:MouseEvent ) : void {
			_RemoveLabelUi();
		}
		
		private function _Target_OnRemovedFromStage ( p_event:Event ) : void {
			_Destroy();
		}
		
		 private function _Stage_OnResize ( p_event:Event ) : void {
			_SetStageWidth();
		}
		
		protected function _SetStageWidth () : void {
			_StageWidth = Fleaxy.$Current.$Stage.stageWidth;
		}
		
		protected function _Repos () : void {
			if ( _LabelUi != null ) {
				var mainStage : Stage = Fleaxy.$Current.$Stage;
				_LabelUi.y = mainStage.mouseY;
				_LabelUi.x = mainStage.mouseX + 15;
//trace(_LabelUi.x + "_" +_LabelUi.y)
				if ( _LabelUi.width + _LabelUi.x + 5 > _StageWidth ) {
					_LabelUi.x = _StageWidth - _LabelUi.width - 5;
					_LabelUi.y -= 20;
				}
				if ( _LabelUi.y < 0 ) {
					_LabelUi.y = 0;
				}
				else if ( _LabelUi.y + _LabelUi.height > mainStage.stageHeight ) {
					_LabelUi.y = mainStage.stageHeight - _LabelUi.height - 5;
				}
			}
		}
		
		private function _RemoveLabelUi():void {
			_Target.removeEventListener(MouseEvent.MOUSE_OUT, _Target_OnMouseOut);
			_Target.removeEventListener(MouseEvent.MOUSE_MOVE, _Target_OnMouseMove);
			Fleaxy.$Current.$Stage.removeEventListener( Event.RESIZE, _Stage_OnResize );
			
			Fleaxy.$Current.$Stage.removeChild( _LabelUi );
			_LabelUi = null;
		}
		
		private function _Destroy () : void {
			_Target.removeEventListener(Event.REMOVED_FROM_STAGE, _Target_OnRemovedFromStage);
			_Target.removeEventListener(MouseEvent.MOUSE_OVER, _Target_OnMouseOver);
			ObjectUtil.$CleanMemory();
		}
		
		public function $Enable () : void {
			_Target.addEventListener(MouseEvent.MOUSE_OVER, _Target_OnMouseOver);
		}
		
		public function $Disable () : void {
			_Target.removeEventListener(MouseEvent.MOUSE_OVER, _Target_OnMouseOver);
			if ( _LabelUi != null ) {
				_RemoveLabelUi();
			}
		}
		
		public function get $HtmlText () : String {
			return _HtmlText;
		}
		
		public function set $HtmlText ( p_htmlText:String ) : void {
			_HtmlText = p_htmlText;
		}
		
		public function get $Style () : String {
			return _Style;
		}
		
		public function set $Style ( p_style:String ) : void {
			_Style = p_style;
		}
		
		
	}
	
}